package me.jessyan.mvparms.demo.mvp.ui.activity.store;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;

import com.example.xrecyclerview.XRecyclerView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.base.DefaultAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.bean.MainStore;
import me.jessyan.mvparms.demo.di.component.store.DaggerMainStoreComponent;
import me.jessyan.mvparms.demo.di.module.store.MainStoreModule;
import me.jessyan.mvparms.demo.mvp.contract.store.MainStoreContract;
import me.jessyan.mvparms.demo.mvp.presenter.store.MainStorePresenter;
import me.jessyan.mvparms.demo.mvp.ui.activity.integral.MineScoreActivity;
import me.jessyan.mvparms.demo.mvp.ui.adapter.StageredAdapter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 主力店
 */
public class MainStoreActivity extends BaseActivity<MainStorePresenter> implements MainStoreContract.View {

    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainStoreComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .mainStoreModule(new MainStoreModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main_store; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });
        initRecycler();
    }

    private void initRecycler() {

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        recyclerView.setLayoutManager(layoutManager);
        StageredAdapter adapter = new StageredAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.clearHeader();


        ArrayList<MainStore> list = new ArrayList<>();
        list.add(new MainStore("http://f.hiphotos.baidu.com/image/h%3D300/sign=5b0a14ac44ed2e73e3e9802cb701a16d/6a63f6246b600c33ea3425e0144c510fd9f9a14d.jpg"));
        list.add(new MainStore("http://d.hiphotos.baidu.com/image/pic/item/0823dd54564e92586a3754f69e82d158ccbf4ea3.jpg"));
        list.add(new MainStore("http://d.hiphotos.baidu.com/image/h%3D300/sign=51daeafcde3f8794ccff4e2ee21a0ead/728da9773912b31b73cec46d8c18367adbb4e1d0.jpg"));
        list.add(new MainStore("http://b.hiphotos.baidu.com/image/pic/item/37d3d539b6003af32b99a061372ac65c1038b68b.jpg"));
        list.add(new MainStore("http://g.hiphotos.baidu.com/image/h%3D300/sign=a5c4ccabf3dcd100d29cfe21428a47be/78310a55b319ebc42946103f8926cffc1e17165d.jpg"));
        list.add(new MainStore("http://b.hiphotos.baidu.com/image/s%3D550%3Bc%3Dwantu%2C8%2C95/sign=a1736326770e0cf3a4f74efe3a7d9122/908fa0ec08fa513d17b6a2ea386d55fbb2fbd9e2.jpg"));
        list.add(new MainStore("http://d.hiphotos.baidu.com/image/h%3D300/sign=22127027ad44ad3431bf8187e0a30c08/574e9258d109b3de2fb336a2c0bf6c81810a4ce8.jpg"));
        list.add(new MainStore("http://c.hiphotos.baidu.com/image/s%3D550%3Bc%3Dwantu%2C8%2C95/sign=f963501469600c33f479decd2a773232/42166d224f4a20a4d423afc792529822720ed0f5.jpg"));

        list.add(new MainStore("http://f.hiphotos.baidu.com/image/h%3D300/sign=5b0a14ac44ed2e73e3e9802cb701a16d/6a63f6246b600c33ea3425e0144c510fd9f9a14d.jpg"));
        list.add(new MainStore("http://d.hiphotos.baidu.com/image/pic/item/0823dd54564e92586a3754f69e82d158ccbf4ea3.jpg"));
        list.add(new MainStore("http://d.hiphotos.baidu.com/image/h%3D300/sign=51daeafcde3f8794ccff4e2ee21a0ead/728da9773912b31b73cec46d8c18367adbb4e1d0.jpg"));
        list.add(new MainStore("http://b.hiphotos.baidu.com/image/pic/item/37d3d539b6003af32b99a061372ac65c1038b68b.jpg"));
        list.add(new MainStore("http://g.hiphotos.baidu.com/image/h%3D300/sign=a5c4ccabf3dcd100d29cfe21428a47be/78310a55b319ebc42946103f8926cffc1e17165d.jpg"));
        list.add(new MainStore("http://b.hiphotos.baidu.com/image/s%3D550%3Bc%3Dwantu%2C8%2C95/sign=a1736326770e0cf3a4f74efe3a7d9122/908fa0ec08fa513d17b6a2ea386d55fbb2fbd9e2.jpg"));
        list.add(new MainStore("http://d.hiphotos.baidu.com/image/h%3D300/sign=22127027ad44ad3431bf8187e0a30c08/574e9258d109b3de2fb336a2c0bf6c81810a4ce8.jpg"));
        list.add(new MainStore("http://c.hiphotos.baidu.com/image/s%3D550%3Bc%3Dwantu%2C8%2C95/sign=f963501469600c33f479decd2a773232/42166d224f4a20a4d423afc792529822720ed0f5.jpg"));

        adapter.setDatas(list);

        adapter.setOnItemClickListener(new DefaultAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int viewType, Object data, int position) {
                if(view instanceof ImageView){
                    Intent intent = new Intent(MainStoreActivity.this, StoreActivity.class);
//                    intent.putExtra("bean", positionData);
                    ActivityOptionsCompat options =
                            ActivityOptionsCompat.makeSceneTransitionAnimation(MainStoreActivity.this,
                                    view, getResources().getString(R.string.transition_store_img));//与xml文件对应
                    ActivityCompat.startActivity(MainStoreActivity.this, intent, options.toBundle());
                }
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


}
